setfenv(1, require'winapi')
require'winapi.window'
require'winapi.router'

--creation

CB_OKAY             = 0
CB_ERR              = -1
CB_ERRSPACE         = -2

CBS_SIMPLE             = 0x0001
CBS_DROPDOWN           = 0x0002
CBS_DROPDOWNLIST       = 0x0003
CBS_OWNERDRAWFIXED     = 0x0010
CBS_OWNERDRAWVARIABLE  = 0x0020
CBS_AUTOHSCROLL        = 0x0040
CBS_OEMCONVERT         = 0x0080
CBS_SORT               = 0x0100
CBS_HASSTRINGS         = 0x0200
CBS_NOINTEGRALHEIGHT   = 0x0400
CBS_DISABLENOSCROLL    = 0x0800
CBS_UPPERCASE          = 0x2000
CBS_LOWERCASE          = 0x4000

CB_GETEDITSEL                = 0x0140
CB_LIMITTEXT                 = 0x0141
CB_SETEDITSEL                = 0x0142
CB_ADDSTRING                 = 0x0143
CB_DELETESTRING              = 0x0144
CB_DIR                       = 0x0145
CB_GETCOUNT                  = 0x0146
CB_GETCURSEL                 = 0x0147
CB_GETLBTEXT                 = 0x0148
CB_GETLBTEXTLEN              = 0x0149
CB_INSERTSTRING              = 0x014A
CB_RESETCONTENT              = 0x014B
CB_FINDSTRING                = 0x014C
CB_SELECTSTRING              = 0x014D
CB_SETCURSEL                 = 0x014E
CB_SHOWDROPDOWN              = 0x014F
CB_GETITEMDATA               = 0x0150
CB_SETITEMDATA               = 0x0151
CB_GETDROPPEDCONTROLRECT     = 0x0152
CB_SETITEMHEIGHT             = 0x0153
CB_GETITEMHEIGHT             = 0x0154
CB_SETEXTENDEDUI             = 0x0155
CB_GETEXTENDEDUI             = 0x0156
CB_GETDROPPEDSTATE           = 0x0157
CB_FINDSTRINGEXACT           = 0x0158
CB_SETLOCALE                 = 0x0159
CB_GETLOCALE                 = 0x015A
CB_GETTOPINDEX               = 0x015b
CB_SETTOPINDEX               = 0x015c
CB_GETHORIZONTALEXTENT       = 0x015d
CB_SETHORIZONTALEXTENT       = 0x015e
CB_GETDROPPEDWIDTH           = 0x015f
CB_SETDROPPEDWIDTH           = 0x0160
CB_INITSTORAGE               = 0x0161
CB_MULTIPLEADDSTRING         = 0x0163
CB_GETCOMBOBOXINFO           = 0x0164
CB_MSGMAX                    = 0x0165

COMBOBOX_DEFAULTS = {
	class = 'COMBOBOX',
	style = bit.bor(WS_CHILD, WS_VISIBLE, CBS_DROPDOWNLIST),
	x = 10, y = 10, w = 100, h = 100,
}

function CreateComboBox(info)
	info = update({}, COMBOBOX_DEFAULTS, info)
	return CreateWindow(info)
end

--commands

function ComboBox_InsertString(window, pos, s) --returns index
	if not s then pos, s = 0, pos end --pos is optional
	return checkpoz(SNDMSG(window, CB_INSERTSTRING, countfrom0(pos), wcs(s)))
end

function ComboBox_DeleteString(window, pos) --returns count
	return checkpoz(SNDMSG(window, CB_DELETESTRING, countfrom0(pos)))
end

function ComboBox_SetCurSel(window, pos)
	return checkpoz(SNDMSG(window, CB_SETCURSEL, countfrom0(pos)))
end

function ComboBox_SetExtendedUI(window, extended)
	return checkpoz(SNDMSG(window, CB_SETEXTENDEDUI, extended))
end

--showcase

if not ... then
require'winapi.showcase'
local window = ShowcaseWindow()

local cb = CreateComboBox{parent = window, y = 10, w = 200}
ComboBox_InsertString(cb, 'Option #1')
ComboBox_InsertString(cb, 'Option #2')
ComboBox_InsertString(cb, 'Option #3 (2nd option is gone)')
ComboBox_DeleteString(cb, 2)
ComboBox_SetCurSel(cb, 2)
ComboBox_SetExtendedUI(cb, true)

local cb = CreateComboBox{parent = window, y = 40, style = bit.bor(WS_CHILD, WS_VISIBLE, CBS_DROPDOWN)}
SetWindowText(cb, 'Waddaya want?')
ComboBox_InsertString(cb, 'Shotgun')
ComboBox_InsertString(cb, 'Gunpowder')

local cb = CreateComboBox{parent = window, y = 70, w = 200, h = 200, style = bit.bor(WS_CHILD, WS_VISIBLE)}
ComboBox_InsertString(cb, 'Hard as a rock')
ComboBox_InsertString(cb, 'Spongy')
SetWindowText(cb, 'Squishy goodness')

MessageLoop()
end

